//package medicalApp.velociraptors.com;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.JComboBox;

/**
 * Creates the Dialog for Adding a Patient
 * @author Vishnu
 *
 */
@SuppressWarnings("serial")
public class AddPatientDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private final JTextField txtFirstName;
	private final JTextField txtLastName;
	private final JTextField txtUserName;
	private final JTextField txtPassword;
	private final JTextField txtSSN;
	private final Connection con;
	private final JTextField txtDOB_M;
	private final JTextField txtDOB_D;
	private final JTextField txtDOB_Y;
	private final JTextField txtAddress;
	private final JTextField txtPhoneAreaCode;
	private final JTextField txtPhonePart2;
	private final JTextField txtPhonePart3;
	private final JTextField txtEmailAddress;
	private final JTextField txtAge;
	private final JComboBox cmbGender;
	protected int gender;

	/**
	 * Create the dialog.
	 * 
	 * @param mac The MedAppController object that powers this method.
	 */
	public AddPatientDialog(final MedAppController mac) {
		con = mac.makeConnection();
		setBounds(100, 100, 686, 801);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		final JLabel lblFirstName = new JLabel("First Name:");
		lblFirstName.setBounds(37, 48, 114, 14);
		contentPanel.add(lblFirstName);
		
		final JLabel lblLastName = new JLabel("Last Name: ");
		lblLastName.setBounds(37, 110, 75, 14);
		contentPanel.add(lblLastName);
		
		final JLabel lblUsername = new JLabel("Username: ");
		lblUsername.setBounds(37, 172, 98, 14);
		contentPanel.add(lblUsername);
		
		final JLabel lblPassword = new JLabel("Password");
		lblPassword.setBounds(37, 234, 98, 14);
		contentPanel.add(lblPassword);
		
		final JLabel lblSsn = new JLabel("SSN: ");
		lblSsn.setBounds(37, 358, 46, 14);
		contentPanel.add(lblSsn);
		
		txtFirstName = new JTextField();
		txtFirstName.setBounds(306, 40, 245, 22);
		contentPanel.add(txtFirstName);
		txtFirstName.setColumns(10);
		
		txtLastName = new JTextField();
		txtLastName.setBounds(306, 102, 245, 22);
		contentPanel.add(txtLastName);
		txtLastName.setColumns(10);
		
		txtUserName = new JTextField();
		txtUserName.setBounds(306, 164, 245, 22);
		contentPanel.add(txtUserName);
		txtUserName.setColumns(10);
		
		txtPassword = new JTextField();
		txtPassword.setBounds(306, 226, 245, 22);
		contentPanel.add(txtPassword);
		txtPassword.setColumns(10);
		
		txtSSN = new JTextField();
		txtSSN.setBounds(306, 350, 245, 22);
		contentPanel.add(txtSSN);
		txtSSN.setColumns(9);
		
		final JLabel lblDob = new JLabel("DOB");
		lblDob.setBounds(37, 296, 46, 14);
		contentPanel.add(lblDob);
		
		txtDOB_M = new JTextField();
		txtDOB_M.addFocusListener(new FocusAdapter() {
			/**
			 * Sets the focus for the text box.
			 * 
			 * @param arg0 The FocusEvent that triggers the method call
			 */ 
			@Override
			public void focusGained(FocusEvent arg0) {
				if (txtDOB_M.getText().equals("MM"))
					txtDOB_M.setText("");
			}
		});
		txtDOB_M.setText("MM");
		txtDOB_M.setBounds(306, 288, 66, 22);
		contentPanel.add(txtDOB_M);
		txtDOB_M.setColumns(10);
		
		txtDOB_D = new JTextField();
		txtDOB_D.addFocusListener(new FocusAdapter() {
			/**
			 * Sets the focus for the text box.
			 * 
			 * @param arg0 The FocusEvent that triggers the method call
			 */ 
			@Override
			public void focusGained(FocusEvent arg0) {
				if (txtDOB_D.getText().equals("DD"))
					txtDOB_D.setText("");
			}
		});
		txtDOB_D.setText("DD");
		txtDOB_D.setBounds(395, 288, 66, 22);
		contentPanel.add(txtDOB_D);
		txtDOB_D.setColumns(10);
		
		txtDOB_Y = new JTextField();
		txtDOB_Y.addFocusListener(new FocusAdapter() {
			/**
			 * Sets the focus for the text box.
			 * 
			 * @param arg0 The FocusEvent that triggers the method call
			 */ 
			@Override
			public void focusGained(FocusEvent arg0) {
				if (txtDOB_Y.getText().equals("YYYY"))
					txtDOB_Y.setText("");
			}
		});
		txtDOB_Y.setText("YYYY");
		txtDOB_Y.setBounds(485, 288, 66, 22);
		contentPanel.add(txtDOB_Y);
		txtDOB_Y.setColumns(10);
		
		final JLabel lblAddress = new JLabel("Address");
		lblAddress.setBounds(37, 420, 75, 14);
		contentPanel.add(lblAddress);
		
		final JLabel lblPhone = new JLabel("Phone Number");
		lblPhone.setBounds(37, 482, 114, 14);
		contentPanel.add(lblPhone);
		
		final JLabel lblEmailAddress = new JLabel("Email Address");
		lblEmailAddress.setBounds(37, 544, 98, 14);
		contentPanel.add(lblEmailAddress);
		
		final JLabel lblGender = new JLabel("Gender");
		lblGender.setBounds(37, 606, 75, 14);
		contentPanel.add(lblGender);
		
		final JLabel lblAge = new JLabel("Age");
		lblAge.setBounds(37, 668, 46, 14);
		contentPanel.add(lblAge);
		
		txtAddress = new JTextField();
		txtAddress.setBounds(306, 416, 245, 22);
		contentPanel.add(txtAddress);
		txtAddress.setColumns(10);
		
		txtPhoneAreaCode = new JTextField();
		txtPhoneAreaCode.setBounds(306, 478, 66, 22);
		contentPanel.add(txtPhoneAreaCode);
		txtPhoneAreaCode.setColumns(10);
		
		txtPhonePart2 = new JTextField();
		txtPhonePart2.setBounds(395, 478, 66, 22);
		contentPanel.add(txtPhonePart2);
		txtPhonePart2.setColumns(10);
		
		txtPhonePart3 = new JTextField();
		txtPhonePart3.setBounds(485, 478, 66, 22);
		contentPanel.add(txtPhonePart3);
		txtPhonePart3.setColumns(10);
		
		txtEmailAddress = new JTextField();
		txtEmailAddress.setBounds(306, 540, 245, 22);
		contentPanel.add(txtEmailAddress);
		txtEmailAddress.setColumns(10);
		
		final String[] choices = {"Male", "Female"};
		cmbGender = new JComboBox(choices);
		cmbGender.setSelectedIndex(0);
		cmbGender.addActionListener(new ActionListener() {
			/**
			 * Designed to perform the action specified upon triggering.
			 * 
			 * @param arg0 The ActionEvent that triggers the method call.
			 */ 
			public void actionPerformed(ActionEvent arg0) {
				final JComboBox target = (JComboBox)arg0.getSource();
				
				if(target.getSelectedIndex() == 0){
					gender = 0;
				}
				else
					gender = 1;
			}
		});
		cmbGender.setBounds(306, 600, 245, 20);
		contentPanel.add(cmbGender);
		
		txtAge = new JTextField();
		txtAge.setBounds(306, 664, 88, 22);
		contentPanel.add(txtAge);
		txtAge.setColumns(10);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					/**
					 * Designed to perform the action specified upon triggering.
					 * 
					 * @param arg0 The ActionEvent that triggers the method call.
					 */ 
					public void actionPerformed(ActionEvent arg0) {
						final String firstName = txtFirstName.getText();
						final String lastName = txtLastName.getText();
						final String userName = txtUserName.getText();
						String password = txtPassword.getText();	
						final String DOB = txtDOB_Y.getText() + "-" + txtDOB_M.getText() + "-" + txtDOB_D.getText();
						final String address = txtAddress.getText();
						final Long phone = Long.parseLong(txtPhoneAreaCode.getText() + txtPhonePart2.getText() + txtPhonePart3.getText());
						final String email = txtEmailAddress.getText();
						final int age = Integer.parseInt(txtAge.getText());
						
						long ssn = Long.parseLong(txtSSN.getText());
						
						char[] pass = password.toCharArray();
						for (int i = 0; i < pass.length; i++) pass[i] += 7;
						String newPass = "";
						for (int i = pass.length - 1; i >= 0; i--) newPass += ((Character)pass[i]).toString();
						password = newPass;
						
						try {
							Statement stmt = con.createStatement();
							stmt.executeUpdate("INSERT INTO `velociraptorsattack`.`user` (`USERNAME`, `PASSWORD`, `U_FIRSTNAME`, `U_LASTNAME`, `USERTYPE`, `SUSPENDED`, `U_SSN`) VALUES ('" + userName + "', '" + password + "', '" + firstName + "', '" + lastName + "', 'Patient', '0', '" + ssn + "')");
							stmt.executeUpdate("INSERT INTO `velociraptorsattack`.`patients` (`P_FIRSTNAME`, `P_LASTNAME`, `DOB`, `P_SSN`) VALUES ('" + firstName + "', '" + lastName + "', '" + DOB + "', '" + ssn + "')");
							stmt.executeUpdate("INSERT INTO `velociraptorsattack`.`patient_information` (`ADDRESS`, `PHONE`, `EMAIL`, `GENDER`, `AGE`, `FOR_PATIENTSSN`) VALUES ('" + address + "', '" + phone + "', '" + email + "', '" + gender + "', '" + age + "', '" + ssn + "')");
							Object[] options = {"Ok"};
							int dialog = JOptionPane.showOptionDialog(null, "Patient Created!", "Created!", JOptionPane.YES_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, null);
							if(dialog == JOptionPane.YES_OPTION){
								if (mac.getUserType().equals("Doctor")) mac.setDoctorPatientTableModel();
								else if (mac.getUserType().equals("SysAdmin")) mac.setSysAdminPatientTableModel();
								else  mac.setNursePatientTableModel();
								dispose();
							}
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
							JOptionPane.showMessageDialog(null, "Patient Creation Error","Error!", JOptionPane.ERROR_MESSAGE);
						}
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					/**
					 * Designed to perform the action specified upon triggering.
					 * 
					 * @param arg0 The ActionEvent that triggers the method call.
					 */ 
					public void actionPerformed(ActionEvent arg0) {
						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
}
